﻿using Infrastructure.Attribute;
using ZR.Model;
using ZR.Service;
using ZR.ServiceCore.Model;
using ZR.ServiceCore.Services.IService;

namespace ZR.ServiceCore.Services;

[AppService(ServiceType = typeof(IIpRateLimitLogService), ServiceLifetime = LifeTime.Transient)]
public class IpRateLimitLogService : BaseService<IpRateLimitLog>, IIpRateLimitLogService
{
    public void InsertIpRateLimitLogAsync(IpRateLimitLog ipRateLimitLog)
    { 
        Insertable(ipRateLimitLog).ExecuteReturnSnowflakeId();
    }

    public async Task<PagedInfo<IpRateLimitLog>> SelectIpRateLimitLogPageAsync(IpRateLimitLog ipRateLimitLog, PagerInfo pager)
    {
        RefAsync<int> total = 0;
        var exp = Expressionable.Create<IpRateLimitLog>();
        exp.AndIF(ipRateLimitLog.ClientIp.IsNotEmpty(), it => it.ClientIp == ipRateLimitLog.ClientIp);
        var res = await Queryable()
            .Where(exp.ToExpression())
            .ToPageListAsync(pager.PageNum, pager.PageSize, total);
        var page = new PagedInfo<IpRateLimitLog>
        {
            PageSize = pager.PageSize,
            PageIndex = pager.PageNum,
            Result = res,
            TotalNum = total
        };
        return page;
    }
}